<template>

  <el-dialog
    :title="title"
    :visible.sync="show"
    width="40%"
    @close="close"
  >
    <el-form ref="form" :model="timeTask" :rules="rules" class="form" label-position="right" label-width="96px">
      <el-row>
        <el-col span="12">
          <el-form-item label="任务名称" prop="taskName">
            <el-input v-model="timeTask.taskName" />
          </el-form-item>
        </el-col>
        <el-col span="12">
          <el-form-item label="任务分组" prop="taskGroup">
            <el-select v-model="timeTask.taskGroup" clearable placeholder="请选择">
              <el-option
                v-for="item in items"
                :key="item.itemCode"
                :label="item.itemName"
                :value="item.itemCode"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="调用方法" prop="taskFun">
        <el-input v-model="timeTask.taskFun" />
      </el-form-item>
      <el-row>
        <el-col span="12">
          <el-form-item label="cron表达式" prop="cron">
            <el-input v-model="timeTask.cron" />
          </el-form-item>
        </el-col>
        <el-col span="12">
          <el-form-item label="是否并发" prop="concurrence">
            <el-radio-group v-model="timeTask.concurrence">
              <el-radio-button
                v-for="item in concurrenceOptions"
                :key="item.value"
                :label="item.value"
              >{{ item.label }}</el-radio-button>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="错误策略" prop="stragtery">
        <el-radio-group v-model="timeTask.stragtery">
          <el-radio-button
            v-for="item in stragteryOptions"
            :key="item.itemCode"
            :label="item.itemCode"
          >{{ item.itemName }}</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-radio v-model="timeTask.state" :label="false">停用</el-radio>
        <el-radio v-model="timeTask.state" :label="true">正常</el-radio>
      </el-form-item>
      <el-row class="form-button">
        <el-button type="primary" @click="save">确定</el-button>
        <el-button @click="close">取消</el-button>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { findDictItemByCode } from '@/api/sys/dict-item'
import { find, save, update } from '@/api/monitor/time-task'

export default {
  name: 'MonitorTimeTaskForm',
  props: {
    id: {
      type: Number,
      default: null
    },
    title: {
      type: String,
      default: '添加定时任务'
    },
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      timeTask: { id: '', taskName: '', taskGroup: '', taskFun: '', cron: '', concurrence: false,
        stragtery: '0', state: true },
      options: [{ label: '暂停', value: 0 }, { label: '正常', value: 1 }],
      concurrenceOptions: [{ label: '禁止', value: false }, { label: '允许', value: true }],
      stragteryOptions: [],
      items: [],
      rules: {
        taskName: [
          { required: true, message: '请输入任务名称', trigger: 'blur' }
        ],
        taskFun: [
          { required: true, message: '请输入调用方法', trigger: 'blur' }
        ],
        cron: [
          { required: true, message: '请输入cron表达式', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    id(val) {
      if (val) { this.find() }
    }
  },
  created() {
    this.findItems()
    this.findStragteryOptions()
  },
  methods: {
    close() {
      this.$refs['form'].resetFields()
      this.timeTask = { id: '', taskName: '', taskGroup: '', taskFun: '', cron: '', concurrence: false,
        stragtery: '0', state: true }
      this.$emit('update:show', false)
      this.$emit('update:id', null)
    },
    save() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          if (this.id) {
            await update(this.timeTask)
            this.responseRes()
          } else {
            await save(this.timeTask)
            this.responseRes()
          }
        }
      })
    },
    async find() {
      const { data } = await find(this.id)
      this.timeTask = data
    },
    responseRes() {
      this.close()
      this.$parent.findPage()
    },
    async findItems() {
      const { data } = await findDictItemByCode('time_task_group')
      this.items = data
    },
    async findStragteryOptions() {
      const { data } = await findDictItemByCode('time_task_stragtery')
      this.stragteryOptions = data
    }
  }
}
</script>

<style scoped>

</style>
